package com.ihg.hiex.admin.web.controller.session.during;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.utils.CollectionUtils;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.ihg.hiex.admin.entity.security.Login;
import com.ihg.hiex.admin.service.session.SessionMeetingService;
import com.ihg.hiex.admin.service.session.SessionService;
import com.ihg.hiex.admin.web.resolver.Secure;
import com.ihg.hiex.entity.session.Session;
import com.ihg.hiex.entity.session.meeting.SessionMeeting;
import com.ihg.hiex.entity.user.Assessor;
import com.ihg.hiex.entity.user.Delegate;

/**
 * 
 * @author dan
 *
 */
@Controller
@RequestMapping("/sessions/{sessionId}")
@MenuSetting("menu-sessions")
public class OpeningController {
    
    @Autowired
    private SessionService sessionService;
    
    @Autowired
    private SessionMeetingService sessionMeetingService;
    
    /**
     * Assessor/Delegate Day 1 opening
     * 
     * @param login
     * @param sessionId
     * @return
     */
    @MenuSetting(value = "submenu-session-during-day-one")
    @RequestMapping(path = "/day-one-opening", method = RequestMethod.GET)
    public ModelAndView dayOneList(@Secure Login login, @PathVariable("sessionId") Long sessionId) {
        
        ModelAndView modelAndView = new ModelAndView("session/during/opening/day-one-opening-admin-list");
        
        // 1. load session
        Session session = sessionService.load(Session.class, sessionId);
        modelAndView.addObject("session", session);
        
        // 2. get sessionMeetings
        List<String> meetingNames = Arrays.asList("Assessor Day 1 Opening", "Delegate Day 1 Opening");
        List<SessionMeeting> meetings = sessionMeetingService.list(session, meetingNames);
        Map<String, SessionMeeting> meetingMap = CollectionUtils.constructMapValue(meetings, "name");
        modelAndView.addObject("meetingMap", meetingMap);
        
        // 3 list assessor & delegate
        List<Assessor> assessors = sessionService.listAssessor(session);
        modelAndView.addObject("assessors", assessors);
        
        List<Delegate> delegates = sessionService.listDelegate(session);
        modelAndView.addObject("delegates", delegates);
        
        return modelAndView;
        
    }
    
    /**
     * Day tow Opening
     * 
     * @param sessionId
     * @return
     */
    @MenuSetting(value = "submenu-session-during-day-two")
    @RequestMapping(path = "/day-two-opening", method = RequestMethod.GET)
    public ModelAndView dayTwoList(@PathVariable("sessionId") Long sessionId) {
        
        ModelAndView modelAndView = new ModelAndView("session/during/opening/day-two-opening-admin-list");
        
        // 1. load session
        Session session = sessionService.load(Session.class, sessionId);
        modelAndView.addObject("session", session);
        
        // 2. get meeting
        SessionMeeting meeting = sessionMeetingService.load(session, "Assessor Day 2 Opening");
        modelAndView.addObject("meeting", meeting);
        
        // 3 list assessor
        List<Assessor> assessors = sessionService.listAssessor(session);
        modelAndView.addObject("assessors", assessors);
        
        return modelAndView;
        
    }

}
